<?php

    /**
     * @ignore
     */
    if( defined("apl_".__FILE__)) return;
    define("apl_".__FILE__,"apl_".__FILE__); 
    
    require_once(APL_ROOT."/db/apl_db.php");
    require_once(APL_ROOT."/html/apl_html_table.php");
    
    class AplDbGrid extends AplHtmlTable{
          
        function __construct(){
            parent::__construct();
            $this->Rows = array();
            $this->Titles    = array();
        }
        function __destruct(){
            unset( $this->Rows );
            parent::__destruct();
        }
        public function clear(){
            parent::clearColumns();
            unset( $this->Rows );
        }
        
        public function addDataRow( AplHtmlTableRow $row){
            parent::addRow("Columns",$row);
        }
        public function removeDataRow(){
            parent::removeRow("Columns");
        }
        
        private function buildTitlesTag(){             
            if( isset($this->Rows["Titles"]) )
                return $this->Rows["Titles"]->buildTag();
        }
         
        public function addTitle( AplHtmlTableRow $row ){
            parent::removeRow("Titles");
            parent::addRow("Titles",$row);
        }
        public function removeTitle(){
            parent::removeRow("Titles");
        }
        
        public function openTableTag(){
        }
        public function buildRowTag(){
        }
        public function closeTableTag(){
        }        
        public function addRow(){
        }
        public function removeRow(){
        }
        
        public function buildTag( AplQuery $obj ){
            if( count($this->Rows["Columns"]->Columns) <> $obj->getFieldCount() )
                throw new Exception( "Table colum count differs from the query column count", EX_PRM_NOT_VALID );
                
            $html = parent::openTableTag();
            $html.= $this->buildTitlesTag();
            do{
                $rowData = $obj->fetch();
                for( $i=0;$i<count( $this->Rows["Columns"]->Columns );$i++)
                    $this->Rows["Columns"]->Columns[$i]->Text = $rowData[$this->Rows["Columns"]->Columns[$i]->ColumnName];
                $html.= $this->Rows["Columns"]->buildTag();
            }while( $obj->nextRecord() );
            
            $html .= parent::closeTableTag(); 
            return $html;                     
        }
    }
?>
